package com.reyco1.medusa.display.container
{
	import com.reyco1.medusa.display.SmartSprite;
	
	import flash.display.DisplayObject;
	import flash.events.Event;

	public class Grid extends Container
	{
		private var _columns:int;
		private var _horizontalGap:Number;
		private var _verticalGap:Number;
		
		public function Grid()
		{
			super();
			
			_columns 	   = 4;
			_verticalGap   = 5;
			_horizontalGap = 5;
		}

		/* private */
		
		override protected function build():void
		{
			var ypos:Number = 0;
			var xpos:Number = 0;
			var heightValues:Array  = [];
			
			_height = 0;
			_width  = 0;
			
			for(var i:int = 0; i < numChildren; i++)
			{
				var child:DisplayObject = getChildAt(i);
				child.y = ypos;
				child.x = xpos;
				heightValues.push(child.height);
				xpos += child.width + _horizontalGap;
				
				if((i % _columns) == _columns - 1)
				{
					ypos += Math.max.apply(null, heightValues) + verticalGap;
					xpos = 0;
					heightValues = [];
				}
			}		
			
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		/* properties */
		
		public function get columns():int
		{
			return _columns;
		}

		public function set columns(value:int):void
		{
			_columns = value;
			invalidate();
		}

		public function get horizontalGap():Number
		{
			return _horizontalGap;
		}

		public function set horizontalGap(value:Number):void
		{
			_horizontalGap = value;
			invalidate();
		}

		public function get verticalGap():Number
		{
			return _verticalGap;
		}

		public function set verticalGap(value:Number):void
		{
			_verticalGap = value;
			invalidate();
		}


	}
}